
package com.etc.user.controller;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.etc.entity.Comment;
import com.etc.entity.Ordercomment;
import com.etc.user.service.CommentService;
import com.etc.user.service.OrdercommentService;
import com.etc.util.CommonMessage;
import com.etc.util.PageData;

/**
 * @author afrian
 *
 *         date: 2019年4月30日 早上10点20
 */
@Controller
public class OrdercommentController {

	@Resource(name = "ordercommentService")
	private OrdercommentService ordercommentService;
	@Resource(name = "commentService")
	private CommentService commentService;

	/*
	 * @RequestMapping(value = "listOrdercomment", method = RequestMethod.GET)
	 * 
	 * @ResponseBody public PageData<Ordercomment> listOrdercomment(Integer page,
	 * Integer limit, String content) {
	 * 
	 * if (content == null) { content = ""; }
	 * 
	 * PageData<Ordercomment> pd = ordercommentService.getOrdercommentByPage(page,
	 * limit, content);
	 * 
	 * // 这里返回json格式对象 pd.setCode(0); pd.setMsg("请求成功");
	 * 
	 * return pd;
	 * 
	 * }
	 */
	/**
	 * 后端系统评论管理实现更改评论状态功能
	 * 
	 * @param o
	 * @return
	 */
	@RequestMapping(value = "updateOrdercomment", method = RequestMethod.PUT)
	@ResponseBody
	public CommonMessage updateOrdercomment(@RequestBody Ordercomment o) {

		Ordercomment ordercomment = new Ordercomment();
		ordercomment.setCommentid(o.getCommentid());
		ordercomment.setUsername(o.getUsername());
		ordercomment.setCommentcontent(o.getCommentcontent());
		ordercomment.setCommentstatus(o.getCommentstatus());

		// 这里返回json格式对象
		CommonMessage cm = new CommonMessage();

		boolean flag = ordercommentService.updateOrdercomment(ordercomment);

		if (flag) {
			cm.setMsg("修改成功");
		} else {
			cm.setMsg("修改失败");
		}

		return cm;

	}

	/**
	 * 前端个人中心添加评论和上传图片功能
	 * 
	 * @param request
	 * @param orderid
	 * @param commentid
	 * @param username
	 * @param commentcontent
	 * @return
	 */
	@RequestMapping(value = "updatecomment", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage updatecomment(HttpServletRequest request, Integer orderid, Integer orderstatus,
			Integer commentid, String username, String commentcontent) {

		MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
		MultipartFile file = req.getFile("commentfile");
		String leixing = file.getOriginalFilename();
		String type = "jpg,jpeg,png";
		String upload = leixing.substring(leixing.indexOf(".") + 1, leixing.length());

		Ordercomment ordercomment = new Ordercomment();
		CommonMessage cm = new CommonMessage();

		if (type.contains(upload)) {
			String despath = "C:/Users/Administrator/git/hotel/src/main/webapp/imgs" + "/" + file.getOriginalFilename();
			ordercomment.setOrderid(orderid);
			ordercomment.setOrderstatus(orderstatus);
			ordercomment.setCommentid(commentid);
			ordercomment.setUsername(username);
			ordercomment.setCommentcontent(commentcontent);
			ordercomment.setCommentfile(file.getOriginalFilename());
			File desfile = new File(despath);
			File desfile1 = new File("C:/Users/Administrator/git/hotel/src/main/webapp/imgs");
			/*
			 * // 判断路径是否存在，如果不存在就创建一个 if (!desfile1.exists()) { desfile1.mkdirs(); }
			 */
			try {
				FileUtils.copyInputStreamToFile(file.getInputStream(), desfile);

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		boolean flag = ordercommentService.updatecomment(ordercomment);

		if (flag) {
			cm.setMsg("修改成功");
		} else {
			cm.setMsg("修改失败");
		}
		return cm;

	}

	/**
	 * 后端评论管理分页模糊查询功能实现
	 * 
	 * @param page
	 * @param limit
	 * @param content
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "listOrdercomment", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Ordercomment> getOrdercommentByPage(Integer page, Integer limit, String content,
			HttpServletRequest request) throws ServletException, IOException {

		if (content == null) {
			content = "";
		}

		PageData<Ordercomment> pd = ordercommentService.selectOrdercommentByPage(page, limit, content);

		// 这里返回json格式对象
		pd.setCode(0);
		pd.setMsg("请求成功");
		request.getSession().setAttribute("pd", pd);
		return pd;

	}

	/**
	 * 前端个人中心订单列表展示
	 * 
	 * @param page
	 * @param limit
	 * @param content
	 * @param request
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping(value = "listcomment/{user.username}", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Ordercomment> getcommentByPage(Integer page, Integer limit,
			@PathVariable(value = "user.username") String content, HttpServletRequest request)
			throws ServletException, IOException {

		if (content == null) {
			content = "";
		}

		PageData<Ordercomment> ss = ordercommentService.selectcommentByPage(page, 10000, content);

		// 这里返回json格式对象
		ss.setCode(0);
		ss.setMsg("请求成功");
		request.getSession().setAttribute("ss", ss);
		return ss;

	}

	/**
	 * 修订 前端个人中心订单评价功能实现，之前逻辑思维考虑不周全导致功能上存在问题，评论表无法同步更新获取到相应的订单消息从而自动增加评论ID
	 * 
	 * @param request
	 * @param orderid
	 * @param orderstatus
	 * @param commentid
	 * @param username
	 * @param commentcontent
	 * @return
	 */

	@RequestMapping(value = "editcomments", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage editcomment(HttpServletRequest request, Integer orderid, Integer userid, Integer orderstatus,
			Integer commentid, String username, String commentcontent) {

		MultipartHttpServletRequest req = (MultipartHttpServletRequest) request;
		MultipartFile file = req.getFile("commentfile");
		String commentfile = file.getOriginalFilename();
		String type = "jpg,jpeg,png";
		String upload = commentfile.substring(commentfile.indexOf(".") + 1, commentfile.length());

		Ordercomment ordercomment = new Ordercomment();
		Comment comment=new Comment(userid, commentcontent, commentfile, orderid);
		CommonMessage cm = new CommonMessage();

		if (orderstatus == 1) {
			cm.setMsg("当前为入住状态无法对此订单进行评价");
			return cm;
		}
		if (orderstatus == 2) {
			cm.setMsg("当前为预定状态无法对此订单进行评价");
			return cm;
		}
		if (orderstatus == 3) {
			cm.setMsg("当前订单已取消无法对此订单进行评价");
			return cm;
		}

		if (orderstatus == 0) {
			// 这里返回json格式对象
			if (type.contains(upload)) {
				String despath = "C:/Users/Administrator/git/hotel/src/main/webapp/imgs" + "/"
						+ file.getOriginalFilename();
				ordercomment.setOrderid(orderid);
				ordercomment.setUserid(userid);
				ordercomment.setOrderstatus(orderstatus);
				ordercomment.setCommentid(commentid);
				ordercomment.setUsername(username);
				ordercomment.setCommentcontent(commentcontent);
				ordercomment.setCommentfile(file.getOriginalFilename());
				File desfile = new File(despath);
				File desfile1 = new File("C:/Users/Administrator/git/hotel/src/main/webapp/imgs");
				/*
				 * // 判断路径是否存在，如果不存在就创建一个 if (!desfile1.exists()) { desfile1.mkdirs(); }
				 */
				try {
					FileUtils.copyInputStreamToFile(file.getInputStream(), desfile);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			System.out.println(userid+commentcontent+commentfile+orderid);
			/*		boolean flag = commentService.insert(comment);
			*/	
					boolean flag1=commentService.insertSelective(comment);
	
					System.out.println(flag1);
					if (flag1) {
						cm.setMsg("评价成功");
						return cm;
					} else {
						cm.setMsg("评价失败，请联系系统管理员");
						return cm;
					}

		}
		return cm;
	
		

	}
}
